Add support for branch/tag/rev options
authorYehuda Katz <wycats@gmail.com>
Tue, 24 Jun 2014 22:23:03 +0000 (15:23 -0700)
committerYehuda Katz <wycats@gmail.com>
Tue, 24 Jun 2014 22:23:14 +0000 (15:23 -0700)
src/cargo/util/toml.rs
tests/test_cargo_compile_git_deps.rs

index e5fd43ee8da67dd3f5596baf72a6a4531fa8ec35..86ce05f7297be452c798cb96b77fd9e7d3999ead 100644 (file)
@@ -71,6 +71,9 @@ pub struct DetailedTomlDependency {
     version: Option<String>,
     path: Option<String>,
     git: Option<String>,
+    branch: Option<String>,
+    tag: Option<String>,
+    rev: Option<String>
 }
 
 #[deriving(Encodable,Decodable,PartialEq,Clone)]
@@ -122,9 +125,14 @@ impl TomlManifest {
                             (Some(string.clone()), SourceId::for_central())
                         },
                         DetailedDep(ref details) => {
+                            let reference = details.branch.as_ref().map(|b| b.clone())
+                                .or_else(|| details.tag.as_ref().map(|t| t.clone()))
+                                .or_else(|| details.rev.as_ref().map(|t| t.clone()))
+                                .unwrap_or_else(|| "master".to_str());
+
                             let new_source_id = details.git.as_ref().map(|git| {
                                 // TODO: Don't unwrap here
-                                let kind = GitKind("master".to_str());
+                                let kind = GitKind(reference.clone());
                                 let url = url::from_str(git.as_slice()).unwrap();
                                 let source_id = SourceId::new(kind, url);
                                 // TODO: Don't do this for path
index aad1c3ddf857c4ef38da929e5b8120e7680b3f4f..9c50a90c23f4962e21046d7f98377977f35b5c8b 100644 (file)
@@ -101,6 +101,134 @@ test!(cargo_compile_simple_git_dep {
       execs().with_stdout("hello world\n"));
 })
 
+test!(cargo_compile_git_dep_branch {
+    let project = project("foo");
+    let git_project = git_repo("dep1", |project| {
+        project
+            .file("Cargo.toml", r#"
+                [project]
+
+                name = "dep1"
+                version = "0.5.0"
+                authors = ["carlhuda@example.com"]
+
+                [[lib]]
+
+                name = "dep1"
+            "#)
+            .file("src/dep1.rs", r#"
+                pub fn hello() -> &'static str {
+                    "hello world"
+                }
+            "#)
+    }).assert();
+
+    git_project.process("git").args(["checkout", "-b", "branchy"]).exec_with_output().assert();
+    git_project.process("git").args(["branch", "-d", "master"]).exec_with_output().assert();
+
+    let project = project
+        .file("Cargo.toml", format!(r#"
+            [project]
+
+            name = "foo"
+            version = "0.5.0"
+            authors = ["wycats@example.com"]
+
+            [dependencies.dep1]
+
+            git = "file://{}"
+            branch = "branchy"
+
+            [[bin]]
+
+            name = "foo"
+        "#, git_project.root().display()))
+        .file("src/foo.rs", main_file(r#""{}", dep1::hello()"#, ["dep1"]));
+
+    let root = project.root();
+    let git_root = git_project.root();
+
+    assert_that(project.cargo_process("cargo-build"),
+        execs()
+        .with_stdout(format!("{} git repository `file:{}`\n\
+                              {} dep1 v0.5.0 (file:{})\n\
+                              {} foo v0.5.0 (file:{})\n",
+                             UPDATING, git_root.display(),
+                             COMPILING, git_root.display(),
+                             COMPILING, root.display()))
+        .with_stderr(""));
+
+    assert_that(&project.root().join("target/foo"), existing_file());
+
+    assert_that(
+      cargo::util::process("foo").extra_path(project.root().join("target")),
+      execs().with_stdout("hello world\n"));
+})
+
+test!(cargo_compile_git_dep_tag {
+    let project = project("foo");
+    let git_project = git_repo("dep1", |project| {
+        project
+            .file("Cargo.toml", r#"
+                [project]
+
+                name = "dep1"
+                version = "0.5.0"
+                authors = ["carlhuda@example.com"]
+
+                [[lib]]
+
+                name = "dep1"
+            "#)
+            .file("src/dep1.rs", r#"
+                pub fn hello() -> &'static str {
+                    "hello world"
+                }
+            "#)
+    }).assert();
+
+    git_project.process("git").args(["tag", "v0.1.0"]).exec_with_output().assert();
+    git_project.process("git").args(["checkout", "-b", "tmp"]).exec_with_output().assert();
+    git_project.process("git").args(["branch", "-d", "master"]).exec_with_output().assert();
+
+    let project = project
+        .file("Cargo.toml", format!(r#"
+            [project]
+
+            name = "foo"
+            version = "0.5.0"
+            authors = ["wycats@example.com"]
+
+            [dependencies.dep1]
+
+            git = "file://{}"
+            tag = "v0.1.0"
+
+            [[bin]]
+
+            name = "foo"
+        "#, git_project.root().display()))
+        .file("src/foo.rs", main_file(r#""{}", dep1::hello()"#, ["dep1"]));
+
+    let root = project.root();
+    let git_root = git_project.root();
+
+    assert_that(project.cargo_process("cargo-build"),
+        execs()
+        .with_stdout(format!("{} git repository `file:{}`\n\
+                              {} dep1 v0.5.0 (file:{})\n\
+                              {} foo v0.5.0 (file:{})\n",
+                             UPDATING, git_root.display(),
+                             COMPILING, git_root.display(),
+                             COMPILING, root.display()))
+        .with_stderr(""));
+
+    assert_that(&project.root().join("target/foo"), existing_file());
+
+    assert_that(
+      cargo::util::process("foo").extra_path(project.root().join("target")),
+      execs().with_stdout("hello world\n"));
+})
 test!(cargo_compile_with_nested_paths {
     let git_project = git_repo("dep1", |project| {
         project